package com.mdd.front.service.impl.zdjd;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.zdjd.TZdjdDevice;
import com.mdd.common.entity.zdjd.param.DeviceFeedbackDataParam;
import com.mdd.common.mapper.zdjd.TZdjdDeviceMapper;
import com.mdd.common.util.TimeUtils;
import com.mdd.front.service.zdjd.ITZdjdDeviceService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.zdjd.TZdjdDeviceCreateValidate;
import com.mdd.front.validate.zdjd.TZdjdDeviceSearchValidate;
import com.mdd.front.validate.zdjd.TZdjdDeviceUpdateValidate;
import com.mdd.front.vo.zdjd.TZdjdDeviceDetailVo;
import com.mdd.front.vo.zdjd.TZdjdDeviceListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 设备实现类
 * @author LikeAdmin
 */
@Service
public class TZdjdDeviceServiceImpl implements ITZdjdDeviceService {
        
    @Resource
    TZdjdDeviceMapper tZdjdDeviceMapper;

    /**
     * 设备列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TZdjdDeviceListedVo>
     */
    @Override
    public PageResult<TZdjdDeviceListedVo> list(PageValidate pageValidate, TZdjdDeviceSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TZdjdDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tZdjdDeviceMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:name:str",
            "=:status:int",
            "=:agentId@agent_id:int",
            "like:agentName@agent_name:str",
            "=:type:int",
            "=:year:str",
            "=:month:str",
            "=:day:str",
            "=:runTotalTime@run_total_time:long",
            "=:totalEnergyConsumption@total_energy_consumption:long",
        });

        IPage<TZdjdDevice> iPage = tZdjdDeviceMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TZdjdDeviceListedVo> list = new LinkedList<>();
        for(TZdjdDevice item : iPage.getRecords()) {
            TZdjdDeviceListedVo vo = new TZdjdDeviceListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 设备详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TZdjdDevice
     */
    @Override
    public TZdjdDeviceDetailVo detail(Integer id) {
        TZdjdDevice model = tZdjdDeviceMapper.selectOne(
                new QueryWrapper<TZdjdDevice>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TZdjdDeviceDetailVo vo = new TZdjdDeviceDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 设备新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TZdjdDeviceCreateValidate createValidate) {
        TZdjdDevice model = new TZdjdDevice();
        model.setName(createValidate.getName());
        model.setStatus(createValidate.getStatus());
        model.setAgentId(createValidate.getAgentId());
        model.setAgentName(createValidate.getAgentName());
        model.setType(createValidate.getType());
        model.setCreateTime(new Date());
        model.setYear(createValidate.getYear());
        model.setMonth(createValidate.getMonth());
        model.setDay(createValidate.getDay());
        model.setUpdateTime(new Date());
        model.setRunTotalTime(createValidate.getRunTotalTime());
        model.setTotalEnergyConsumption(createValidate.getTotalEnergyConsumption());
        tZdjdDeviceMapper.insert(model);
    }

    /**
     * 设备编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TZdjdDeviceUpdateValidate updateValidate) {
        TZdjdDevice model = tZdjdDeviceMapper.selectOne(
                new QueryWrapper<TZdjdDevice>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setStatus(updateValidate.getStatus());
        model.setAgentId(updateValidate.getAgentId());
        model.setAgentName(updateValidate.getAgentName());
        model.setType(updateValidate.getType());
        model.setYear(updateValidate.getYear());
        model.setMonth(updateValidate.getMonth());
        model.setDay(updateValidate.getDay());
        model.setUpdateTime(new Date());
        model.setRunTotalTime(updateValidate.getRunTotalTime());
        model.setTotalEnergyConsumption(updateValidate.getTotalEnergyConsumption());
        tZdjdDeviceMapper.updateById(model);
    }

    /**
     * 设备删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TZdjdDevice model = tZdjdDeviceMapper.selectOne(
                new QueryWrapper<TZdjdDevice>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tZdjdDeviceMapper.delete(new QueryWrapper<TZdjdDevice>().eq("id", id));
    }

    /**
     * 根据设备号查询设备
     * @author LikeAdmin
     * @param deviceCode 设备号
     * @return TZdjdDevice
     */
    @Override
    public TZdjdDevice selectDeviceByCode(String deviceCode) {
        TZdjdDevice device = tZdjdDeviceMapper.selectOne(new QueryWrapper<TZdjdDevice>().eq("device_code", deviceCode).last("limit 1"));
        return Optional.ofNullable(device).orElse(new TZdjdDevice());
    }

    /**
     * 添加新设备数据
     * @author LikeAdmin
     * @param device
     */
    @Override
    public void insertDevice(TZdjdDevice device) {
        tZdjdDeviceMapper.insert(device);
    }

    @Override
    public List<TZdjdDevice> selectAll() {
        return Optional.ofNullable(tZdjdDeviceMapper.selectList(new QueryWrapper<>())).orElse(new ArrayList<>());
    }

    /**
     * 根据回传的数据的设备唯一标识uuid和设备型号查询设备信息
     * @author LikeAdmin
     * @param uuid 设备唯一id
     * @param model 设备型号
     * @return TZdjdDevice
     */
    @Override
    public TZdjdDevice selectDeviceByUuidAndCode(String uuid, String model) {
        QueryWrapper<TZdjdDevice> wrapper = new QueryWrapper<TZdjdDevice>().eq("device_uuid", uuid).eq("device_code", model);
        TZdjdDevice device = tZdjdDeviceMapper.selectOne(wrapper);
        return Optional.ofNullable(device).orElse(new TZdjdDevice());
    }

    @Override
    public void updateById(TZdjdDevice device) {
        tZdjdDeviceMapper.updateById(device);
    }

}
